<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: Script builder</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_addon_build.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Script builder </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/scriptbuilder/</p>
<p>This class is a helper class for loading and building scripts, with a basic pre-processor that supports conditional compilation, include directives, and metadata declarations.</p>
<p>By default the script builder resolves include directives by loading the included file from the relative directory of the file it is included from. If you want to do this in another way, then you should implement the <a class="el" href="doc_addon_build.html#doc_addon_build_1_1">include callback</a> which will let you process the include directive in a custom way, e.g. to load the included file from memory, or to support multiple search paths. The include callback should call the AddSectionFromFile or AddSectionFromMemory to include the section in the current build.</p>
<p>If you do not want process metadata then you can compile the add-on with the define AS_PROCESS_METADATA 0, which will exclude the code for processing this. This define can be made in the project settings or directly in the header.</p>
<h1><a class="anchor" id="doc_addon_build_1"></a>
Public C++ interface</h1>
<div class="fragment"><div class="line"><span class="keyword">class </span>CScriptBuilder</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">  <span class="comment">// Start a new module</span></div>
<div class="line">  <span class="keywordtype">int</span> StartNewModule(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine, <span class="keyword">const</span> <span class="keywordtype">char</span> *moduleName);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Load a script section from a file on disk</span></div>
<div class="line">  <span class="comment">// Returns  1 if the file was included</span></div>
<div class="line">  <span class="comment">//          0 if the file had already been included before</span></div>
<div class="line">  <span class="comment">//         &lt;0 on error</span></div>
<div class="line">  <span class="keywordtype">int</span> AddSectionFromFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Load a script section from memory</span></div>
<div class="line">  <span class="comment">// Returns  1 if the section was included</span></div>
<div class="line">  <span class="comment">//          0 if a section with the same name had already been included before</span></div>
<div class="line">  <span class="comment">//         &lt;0 on error</span></div>
<div class="line">  <span class="keywordtype">int</span> AddSectionFromMemory(<span class="keyword">const</span> <span class="keywordtype">char</span> *sectionName,</div>
<div class="line">                           <span class="keyword">const</span> <span class="keywordtype">char</span> *scriptCode, </div>
<div class="line">                           <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> scriptLength = 0,</div>
<div class="line">                           <span class="keywordtype">int</span> lineOffset = 0);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Build the added script sections</span></div>
<div class="line">  <span class="keywordtype">int</span> BuildModule();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Returns the current module</span></div>
<div class="line">  <a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *GetModule();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Register the callback for resolving include directive</span></div>
<div class="line">  <span class="keywordtype">void</span> SetIncludeCallback(INCLUDECALLBACK_t callback, <span class="keywordtype">void</span> *userParam);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Add a pre-processor define for conditional compilation</span></div>
<div class="line">  <span class="keywordtype">void</span> DefineWord(<span class="keyword">const</span> <span class="keywordtype">char</span> *word);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Enumerate included script sections</span></div>
<div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> GetSectionCount() <span class="keyword">const</span>;</div>
<div class="line">  <span class="keywordtype">string</span>       GetSectionName(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> idx) <span class="keyword">const</span>;</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// Get metadata declared for class types and interfaces</span></div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForType(<span class="keywordtype">int</span> typeId);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Get metadata declared for functions</span></div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForFunc(<a class="code" href="classas_i_script_function.html" title="The interface for a script function description.">asIScriptFunction</a> *func);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Get metadata declared for global variables</span></div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForVar(<span class="keywordtype">int</span> varIdx);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Get metadata declared for a class method</span></div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForTypeMethod(<span class="keywordtype">int</span> typeId, <span class="keywordtype">int</span> mthdIdx);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Get metadata declared for a class property</span></div>
<div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForTypeProperty(<span class="keywordtype">int</span> typeId, <span class="keywordtype">int</span> varIdx);</div>
<div class="line">};</div>
</div><!-- fragment --><h2><a class="anchor" id="doc_addon_build_1_1"></a>
The include callback signature</h2>
<div class="fragment"><div class="line"><span class="comment">// This callback will be called for each #include directive encountered by the</span></div>
<div class="line"><span class="comment">// builder. The callback should call the AddSectionFromFile or AddSectionFromMemory</span></div>
<div class="line"><span class="comment">// to add the included section to the script. If the include cannot be resolved</span></div>
<div class="line"><span class="comment">// then the function should return a negative value to abort the compilation.</span></div>
<div class="line"><span class="keyword">typedef</span> int (*INCLUDECALLBACK_t)(<span class="keyword">const</span> <span class="keywordtype">char</span> *include, <span class="keyword">const</span> <span class="keywordtype">char</span> *from, CScriptBuilder *builder, <span class="keywordtype">void</span> *userParam);</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_addon_build_2"></a>
Include directives</h1>
<p>Example script with include directive:</p>
<pre>
  #include "commonfuncs.as"</pre><pre>  void main()
  {
    // Call a function from the included file
    CommonFunc();
  }
</pre><h1><a class="anchor" id="doc_addon_build_condition"></a>
Conditional programming</h1>
<p>The builder supports conditional programming through the #if/#endif preprocessor directives. The application may define a word with a call to DefineWord(), then the scripts may check for this definition in the code in order to include/exclude parts of the code.</p>
<p>This is especially useful when scripts are shared between different binaries, for example, in a client/server application.</p>
<p>Example script with conditional compilation:</p>
<pre>
  class CObject
  {
    void Process()
    {
  #if SERVER
      // Do some server specific processing
  #endif</pre><pre>  #if CLIENT
      // Do some client specific processing
  #endif</pre><pre>      // Do some common processing
    }
  }
</pre><h1><a class="anchor" id="doc_addon_build_metadata"></a>
Metadata in scripts</h1>
<p>Metadata can be added before script class, interface, function, and global variable declarations. The metadata is removed from the script by the builder class and stored for post build lookup by the type id, function id, or variable index.</p>
<p>Exactly what the metadata looks like is up to the application. The builder class doesn't impose any rules, except that the metadata should be added between brackets []. After the script has been built the application can obtain the metadata strings and interpret them as it sees fit.</p>
<p>Example script with metadata:</p>
<pre>
  [factory func = CreateOgre]
  class COgre
  {
    [editable] 
    vector3 myPosition;</pre><pre>    [editable [10, 100]]
    int     myStrength;
  }</pre><pre>  [factory]
  COgre @CreateOgre()
  {
    return @COgre();
  }
</pre><p>Example usage:</p>
<div class="fragment"><div class="line">CScriptBuilder builder;</div>
<div class="line"><span class="keywordtype">int</span> r = builder.StartNewModule(engine, <span class="stringliteral">&quot;my module&quot;</span>);</div>
<div class="line"><span class="keywordflow">if</span>( r &gt;= 0 )</div>
<div class="line">  r = builder.AddSectionFromMemory(script);</div>
<div class="line"><span class="keywordflow">if</span>( r &gt;= 0 )</div>
<div class="line">  r = builder.BuildModule();</div>
<div class="line"><span class="keywordflow">if</span>( r &gt;= 0 )</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// Find global variables that have been marked as editable by user</span></div>
<div class="line">  <a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *mod = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4" title="Return an interface pointer to the module.">GetModule</a>(<span class="stringliteral">&quot;my module&quot;</span>);</div>
<div class="line">  <span class="keywordtype">int</span> count = mod-&gt;<a class="code" href="classas_i_script_module.html#a87e29773f7e6f2980d75288faaa74d02" title="Returns the number of global variables in the module.">GetGlobalVarCount</a>();</div>
<div class="line">  <span class="keywordflow">for</span>( <span class="keywordtype">int</span> n = 0; n &lt; count; n++ )</div>
<div class="line">  {</div>
<div class="line">    <span class="keywordtype">string</span> metadata = builder.GetMetadataStringForVar(n);</div>
<div class="line">    <span class="keywordflow">if</span>( metadata == <span class="stringliteral">&quot;editable&quot;</span> )</div>
<div class="line">    {</div>
<div class="line">      <span class="comment">// Show the global variable in a GUI</span></div>
<div class="line">      ...</div>
<div class="line">    }</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:27 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
